from flask import Blueprint, render_template, session, g, redirect, url_for, jsonify
from models import Teacher, Student

bp = Blueprint("teacher", __name__, url_prefix="/teacher")


@bp.route("/")
def teacher_index():
    user = g.get("user")
    print(user)
    if user:
        return render_template('main.html')
    else:
        return redirect(url_for('login.login'))


@bp.route("/list")
def teacher_list():
    print("学生列表")
    # 1。get查找 根据主键查找
    # teacher = Teacher.query.get(1001)
    # print(teacher.name)
    # 2。fliter_by查找
    # students = Student.query.filter_by(class_id = "230101")
    teachers = Teacher.query.all()
    return render_template('teacher.html', teachers=teachers)


@bp.before_request
def before_request():
    """教师管理蓝图权限检查"""
    # 检查用户是否登录
    if session.get('is_login') != 'true':
        return redirect('/login')
    
    # 教师功能只允许教师角色访问
    user_role = session.get('role')
    if user_role != 'teacher':
        return redirect('/login')
